package com.lw.biz.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lw.base.client.result.BaseResult;
import com.lw.biz.client.model.BizDataModel;
import com.lw.biz.client.service.IBizDataService;
import com.lw.biz.dal.dao.BizDataDao;
import com.lw.biz.dal.entity.BizData;
import com.lw.biz.dal.enums.GtOrLtEnum;
import com.lw.biz.dal.enums.SortEnum;

@Service("iBizDataService")
public class IBizDataServiceImpl implements IBizDataService {

	@Autowired
	private BizDataDao bizDataDao;

	public BaseResult<List<BizDataModel>> getListGtIdAndDesc(Long id, String categoryCode, int offset) {
		try {
			List<BizData> dbList = bizDataDao.selectDataByOffset(id, categoryCode, offset, GtOrLtEnum.GT, SortEnum.DESC);
			List<BizDataModel> reList = new ArrayList<BizDataModel>();
			for(BizData bizData : dbList){
				BizDataModel bizDataModel = new BizDataModel();
				BeanUtils.copyProperties(bizData, bizDataModel);
				reList.add(bizDataModel);
			}
			return new BaseResult<List<BizDataModel>>(reList);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult<List<BizDataModel>>(null,BaseResult.FAIL,false,e.getMessage());
		}
	}

	public BaseResult<List<BizDataModel>> getListGtIdAndAsc(Long id, String categoryCode, int offset) {
		try {
			List<BizData> dbList = bizDataDao.selectDataByOffset(id, categoryCode, offset, GtOrLtEnum.GT, SortEnum.ASC);
			List<BizDataModel> reList = new ArrayList<BizDataModel>();
			for(BizData bizData : dbList){
				BizDataModel bizDataModel = new BizDataModel();
				BeanUtils.copyProperties(bizData, bizDataModel);
				reList.add(bizDataModel);
			}
			return new BaseResult<List<BizDataModel>>(reList);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult<List<BizDataModel>>(null,BaseResult.FAIL,false,e.getMessage());
		}
	}

	public BaseResult<List<BizDataModel>> getListLtIdAndDesc(Long id, String categoryCode, int offset) {
		try {
			List<BizData> dbList = bizDataDao.selectDataByOffset(id, categoryCode, offset, GtOrLtEnum.LT, SortEnum.DESC);
			List<BizDataModel> reList = new ArrayList<BizDataModel>();
			for(BizData bizData : dbList){
				BizDataModel bizDataModel = new BizDataModel();
				BeanUtils.copyProperties(bizData, bizDataModel);
				reList.add(bizDataModel);
			}
			return new BaseResult<List<BizDataModel>>(reList);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult<List<BizDataModel>>(null,BaseResult.FAIL,false,e.getMessage());
		}
	}

	public BaseResult<List<BizDataModel>> getListLtIdAndAsc(Long id, String categoryCode, int offset) {
		try {
			List<BizData> dbList = bizDataDao.selectDataByOffset(id, categoryCode, offset, GtOrLtEnum.LT, SortEnum.ASC);
			List<BizDataModel> reList = new ArrayList<BizDataModel>();
			for(BizData bizData : dbList){
				BizDataModel bizDataModel = new BizDataModel();
				BeanUtils.copyProperties(bizData, bizDataModel);
				reList.add(bizDataModel);
			}
			return new BaseResult<List<BizDataModel>>(reList);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult<List<BizDataModel>>(null,BaseResult.FAIL,false,e.getMessage());
		}
	}
}
